<?php

/**
 * commentaire actions.
 *
 * @package    TC5
 * @subpackage commentaire
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class commentaireActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  	$commentaire = new CommentairePeer();
  	$this->commentaire_list = $commentaire->getCommentaireListByProduit($request);
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->commentaire = CommentairePeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->commentaire);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new CommentaireForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));
	if ($request->hasParameter("produit_id") && $request->hasParameter("produit_id") && $request->hasParameter("new_com") && $request->getParameter("new_com") != "") {
		$com = new Commentaire();
		$com->setNote($request->getParameter("note"));
		$com->setPersonne(PersonnePeer::retrieveByPK($request->getParameter("personne_id")));
		$com->setProduit(ProduitPeer::retrieveByPK($request->getParameter("produit_id")));
		$com->setCommentaire($request->getParameter("new_com"));
		$com->save();
		
	}
	$this->redirect('produit/show?id='.$request->getParameter('produit_id'));
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($commentaire = CommentairePeer::retrieveByPk($request->getParameter('id')), sprintf('Object commentaire does not exist (%s).', $request->getParameter('id')));
    $this->form = new CommentaireForm($commentaire);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($commentaire = CommentairePeer::retrieveByPk($request->getParameter('id')), sprintf('Object commentaire does not exist (%s).', $request->getParameter('id')));
    $this->form = new CommentaireForm($commentaire);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($commentaire = CommentairePeer::retrieveByPk($request->getParameter('id')), sprintf('Object commentaire does not exist (%s).', $request->getParameter('id')));
    $commentaire->delete();

    $this->redirect('commentaire/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $commentaire = $form->save();

      $this->redirect('commentaire/edit?id='.$commentaire->getId());
    }
  }
}
